#ifndef __MC_MATH_H_
#define __MC_MATH_H_


#ifdef __cplusplus
extern "C"{
#endif
    
#include "mc_type.h"

#define _IQfvalue(fvalue,Qvalue)   ((s16)((float)fvalue*((1<<Qvalue) - 1))) 
#define _IQmpy(A,B,Q)       ((A)*(B)>>Q)
#define _IQsat(A,Max,Min)   ((A)>Max?Max:(A<Min?Min:A))
#define _IQabs(A)           ((A)>=0?(A):(-A))
#define _IQsign(A)          ((A)<0?(-1):1)
#define _IQreal2intdec(A,Int,Dec)   {Int=(s16)(A);Dec=((A)-Int)*32767.0;}
#define _IQintdec2real(Int,Dec,A)   {(A)=Int+Dec/32767.0;}
#define _IQtriangle(Angle, Sin, Cos)    {\
    MATH_ACC->MODE = MATH_ACC_MODE_TRIANGLE;\
    MATH_ACC->OP0 = Angle;\
    MATH_ACC->START = MATH_ACC_START_START_Msk;\
    while ((MATH_ACC->SR & (MATH_ACC_SR_BUSY_Msk)) != 0x00u);\
    Sin = (int16_t)MATH_ACC->OP0;\
    Cos = (int16_t)MATH_ACC->OP1;}

#define _IQdivision(Divisor, Dividend, Quotation, Remainder)    {\
    MATH_ACC->MODE = MATH_ACC_MODE_DIVISION;\
    MATH_ACC->OP0 = Dividend;\
    MATH_ACC->OP1 = Divisor;\
    MATH_ACC->START = MATH_ACC_START_START_Msk;\
    while (((MATH_ACC->SR & (MATH_ACC_SR_BUSY_Msk)) != 0x00u));\
    Quotation = (int16_t)MATH_ACC->OP0;\
    Remainder = (int16_t)MATH_ACC->OP1;}
    
#define _IQarcmod(Sin, Cos, Arc, Mod)   {\
    MATH_ACC->MODE = MATH_ACC_MODE_ARCTANGENT_MOD;\
    MATH_ACC->OP0 = Sin;\
    MATH_ACC->OP1 = Cos;\
    MATH_ACC->START = MATH_ACC_START_START_Msk;\
    while ((MATH_ACC->SR & (MATH_ACC_SR_BUSY_Msk)) != 0x00u);\
    Mod = (int16_t)MATH_ACC->OP0;\
    Arc = (int16_t)MATH_ACC->OP1;}

void MC_Clarke(ABC_t ABC, pAlBe_t pAlBe);
void MC_Park(AlBe_t AlBe, pDQ_t pDQ, SinCos_t SinCos);
void MC_IPark(DQ_t DQ, pAlBe_t pAlBe, SinCos_t SinCos);
void MC_IClarke(AlBe_t AlBe, pABC_t pABC);
void MC_IClarkePow(AlBe_t AlBe, pABC_t pABC);

void MC_SoftDelay(u32 cnt);
s16 MC_Filter(s16 In, s16 Out, s16 Filter);
void MC_IntDec(pIntDec_t pIntDec_Handle);
		
#ifdef __cplusplus
}
#endif


#endif

